package com.xjlsoft.weather.common.util;

import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.FileReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.OutputStreamWriter;
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
import java.util.Random;

import org.apache.commons.lang3.StringUtils;

/**
 * 字符串处理工具类
 * @author van.zheng
 *
 */
public class StringUtil extends StringUtils{
	
	

	/**
	 * 去除以逗号分隔的字符串的重复项
	 * @param str
	 * @return
	 */
	public static List<String> RemoveRepeatItem(String str) {
		if (!isNotBlank(str)) {
			return null;
		}
		List<String> list = new ArrayList<String>();
		String[] array = str.split(",");
		for (String s : array) {
			if (Collections.frequency(list, s) < 1) {
				list.add(s);
			}
		}
		return list;
	}

	/**
	 * 数字不足位数左补0
	 * @param str
	 * @param strLength
	 */
	public static String addZeroForNum(String str, int strLength) {
		int strLen = str.length();
		if (strLen < strLength) {
			while (strLen < strLength) {
				StringBuffer sb = new StringBuffer();
				sb.append("0").append(str); // 左补0
				str = sb.toString();
				strLen = str.length();
			}
		}
		return str;
	}

	public static String getRandomCode(){
		String base = "abcdefghijklmnopqrstuvwxyz0123456789";
		Random random = new Random();
		StringBuffer sb = new StringBuffer();
		for (int i = 0; i < 10; i++) {
			int number = random.nextInt(base.length());
			sb.append(base.charAt(number));
		}
		return sb.toString();
	}
	
	/**
	 * 字符串 集合 分割
	 * 示例:
	 *  'list.get(0)','list.get(1)'
	 * */
	 public static String getIdsString(List<String> list) {
			String r = "";
			if (list.size() == 0) {
				return r;
			}
			for (int i = 0; i < list.size(); i++) {
				r += ",'" + list.get(i) + "'";
			}
			r = r.substring(1);
			return r;
		}
	 
	 /**
	  * 读取 磁盘上的 json格式 文档 转为String
	 * @throws IOException 
	  * 
	  * */
	 public static String getJsonTxt(FileReader fileReader) throws IOException{

	      StringBuffer buffer = new StringBuffer();
	      BufferedReader bf= new BufferedReader(fileReader);
	      String s = null;
	      while((s = bf.readLine())!=null){//使用readLine方法，一次读一行
	          buffer.append(s.trim());
	      }
	      String jsonTxt = buffer.toString();
	      return jsonTxt;
	 }
	 
	 /**
		 * @Description : TXT文件编码转换为 UTF8
		 * @author : lj
		 * @param :
		 *            [file]
		 * @return : boolean
		 * @exception :
		 */
		public static boolean tranTxtToUTF8(File file) {
			try {
				FileInputStream fis = new FileInputStream(file);
				InputStreamReader isr = new InputStreamReader(fis, "GBK");
				BufferedReader br = new BufferedReader(isr);
				String OriStr;
				StringBuilder stringBuilder = new StringBuilder();
				while ((OriStr = br.readLine()) != null) {
					// 手动拼接换行符
					OriStr += "\n";
					stringBuilder.append(OriStr);
				}
				String targetStr = stringBuilder.toString();
				// false代表不追加直接覆盖,true代表追加文件
				FileOutputStream fos = new FileOutputStream(file, false);
				OutputStreamWriter osw = new OutputStreamWriter(fos, "UTF-8");
				osw.write(stringBuilder.toString());
				osw.flush();
				osw.close();
				fos.close();
				br.close();
				isr.close();
				fis.close();
				return true;
			} catch (Exception e) {
				// TODO: handle exception
				return false;
			}
		}

}
